package com.aglhz.nature.adapter;

import java.util.List;




import com.aglhz.nature.R;
import com.aglhz.nature.modle.item.classification.ClassificationData;
import com.aglhz.nature.view.SecondClassificationActivity;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 一级分类gridview adapter
 * @author Administrator
 *
 */
public class ClassificationAdapter extends BaseAdapter{
	
	public List<ClassificationData>mData;
    public Context context; 
    
    
	public ClassificationAdapter(List<ClassificationData> mData, Context context) {
		super();
		this.mData = mData;
		this.context = context;
	}

	@Override
	public int getCount() {
	
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
	
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
	
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		 ViewHolder viewHolder; 
		if(null==convertView){			
			  convertView = LayoutInflater.from(context).inflate(R.layout.item_classification, null); 
			  viewHolder = new ViewHolder();
			  viewHolder.image = (ImageView) convertView.findViewById(R.id.iv_classification); 
			  
			  convertView.setTag(viewHolder); 
		}else{
			viewHolder = (ViewHolder) convertView.getTag(); 
		}
		//设置图片
		
			  String imgUrl = mData.get(position).getImage();
			//picasso加载网络图片
			   Picasso.with(context)  
			   .load(imgUrl)  
			   .resize(getScreenWidth()/2, getScreenWidth()*3/8)
			   .into(viewHolder.image) ;
			  
			   
		
//		int screenWidth = 200;
//		int screenHeight=viewHolder.image.getHeight();
//		GridView.LayoutParams p=new GridView.LayoutParams(screenWidth/2,screenWidth/2);
//		convertView.setLayoutParams(p);
		 viewHolder.image.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					
					Intent intent=new Intent(context,SecondClassificationActivity.class);
					intent.putExtra("mcp",mData.get(position).getId());
					context.startActivity(intent);
					
				}
			});
		return convertView;
	}
	class ViewHolder 
	{  
	    public ImageView image; 
;
	} 
	
	/**
	 * 获取屏幕宽度
	 * 
	 * @return
	 */
	protected int getScreenWidth() {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		return display.getWidth();
	}
	
}
